<?php

namespace App\Http\Controllers;

use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    public function is_admin(){
        if (!auth()->user()) throw  new AuthenticationException();
        if (!auth()->user()->is_admin) throw  new AuthenticationException();
    }
    public function RESTfull($data,$status = 200){
        return response()->json($data,$status);
    }
    public function success($data = [],$message = 'success'){
        $res = ['msg'=>$message];
        $res = ($data == [] ? $res:array_merge($res,['data'=>$data]));
        return $this->RESTfull($res,200);
    }
    public function error_422($message = 'data cannot be processed'){
        $data = ['msg'=>$message];
        return $this->RESTfull($data,422);
    }
    public function error_401($message = 'unauthenticated'){
        $data = ['msg'=>$message];
        return $this->RESTfull($data,401);
    }
}
